<template>
	<view class="my">
		<view class='header'>
			<view class="pageHeader dFlex jStart_aCenter" :style="{'height': pageHeader + 'px','padding-top': pageStatus + 'px'}">
				<view class='title'>
					我的
				</view>
			</view>
		</view>
		<view class="userCenter">
			<view class="myBg imgPublic">
				<image mode='widthFix' src=''></image>
				<!-- <image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/myBg.png" mode="widthFix"></image> -->
			</view>
			<view class="userInfoCenter dFlex jBetween_aStart">
				<view class="userInfo dFlex jStart_aCenter" @tap='goPage(5)'>
					<view class="userTx imgPublic">
						<image v-if="!isLogin" src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/mr.png"></image>
						<image v-if="isLogin" :src="datas.avatar"></image>
					</view>
					<view class="info">
						<view class="nickname">{{isLogin?datas.nickname:'HI，欢迎'}}</view>
						<view class='mobile'>{{isLogin?datas.mobile:''}}</view>
					</view>
					<!-- <button class="nickname" v-if="!isLogin" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">登录/注册</button> -->
				</view>
				<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="loginBtn" v-if="!isLogin">登录/注册</button>
				<view class='changeInfo_btn' v-else @tap='goPage(5)'> 设 置 </view>
			</view>
			<!-- 广告位 -->
			<view class="advImg imgPublic">
				<image :src='adv' mode='widthFix'></image>
			</view>
		</view>
		<view class="part">
			<view class="partTit dFlex jBetween_aCenter">
				<text>我的订单</text>
				<!-- <text @tap="lookTG">我的订单</text> -->
				<view class="order dFlex jEnd_aCenter" @tap="goPage(4,'-1')">
					<text>全部订单</text>
					<view class="rightIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/rightIcon.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="dFlex jAround_aCenter orders">
				<view class="menuOne" @tap="goPage(4,'0')" style="width: 20%;">
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/dfk.png" mode="widthFix"></image>
					</view>
					<text>待付款</text>
					<view class="showCount" v-if="orderCount.dfk>0 && isLogin">{{orderCount.dfk?orderCount.dfk:'0'}}</view>
				</view>
				<view class="menuOne" @tap="goPage(4,'10')" style="width: 20%;">
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/dfh.png" mode="widthFix"></image>
					</view>
					<text>待发货</text>
					<view class="showCount" v-if="orderCount.dfh>0 && isLogin">{{orderCount.dfh?orderCount.dfh:'0'}}</view>
				</view>
				<view class="menuOne" @tap="goPage(4,'20')" style="width: 20%;">
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/dsh.png" mode="widthFix"></image>
					</view>
					<text>待收货</text>
					<view class="showCount" v-if="orderCount.dsh>0 && isLogin">{{orderCount.dsh?orderCount.dsh:'0'}}</view>
				</view>
				<!-- <view class="menuOne" @tap="goPage(4,'-2')" style="width: 20%;">
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/dth.png" mode="widthFix"></image>
					</view>
					<text>待提货</text>
					<view class="showCount" v-if="orderCount.dth>0">{{orderCount.dth?orderCount.dth:'0'}}</view>
				</view> -->
				<view class="menuOne" @tap="goPage(4,'60')" style="width: 20%;">
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/ywc.png" mode="widthFix"></image>
					</view>
					<text>已完成</text>
					<view class="showCount" v-if="orderCount.dpj>0 && isLogin">{{orderCount.dpj?orderCount.dpj:'0'}}</view>
				</view>
			</view>
		</view>
		<view class="part">
			<view class="partTit dFlex jBetween_aCenter">
				<text>常用工具</text>
			</view>
			<view class="content orders dFlex jAround_aCenter">
				<view class="menuOne" @tap='goPage(6)' v-if='datas.auth_status != "2"'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/tuanzhang.png" mode="widthFix"></image>
					</view>
					<text>成为团长</text>
				</view>
				<view class="menuOne" @tap='goPage(2)'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/quan.png" mode="widthFix"></image>
					</view>
					<text>优惠券</text>
				</view>
				<view class="menuOne" @tap='goPage(1)'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/address.png" mode="widthFix"></image>
					</view>
					<text>提货人管理</text>
				</view>
				<button style="vertical-align: top;" class="menuOne" hover-class="none" open-type="contact">
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/kefu.png" mode="widthFix"></image>
					</view>
					<text>咨询客服</text>
				</button>
			</view>
		</view>
		<view class="part" v-if='datas.auth_status == "2"'>
			<view class="partTit dFlex jBetween_aCenter">
				<text>团长功能</text>
				<view class="teamBtns dFlex jEnd_aCenter">
					<view class='team_btn' @click='goUrl("/my/warehouse")'>团购商品库</view>
					<view class='team_btn' @click='goUrl("/my/createGroupBuy")'>创建团购</view>
				</view>
			</view>
			<view class="content orders">
				<view class="menuOne" @click='goUrl("/my/wallet")'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/lineIcon.png" mode="widthFix"></image>
					</view>
					<text>团长钱包</text>
				</view>
				<view class="menuOne" @click='goUrl("/my/income")'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/shouyi.png" mode="widthFix"></image>
					</view>
					<text>我的收益</text>
				</view>
				<view class="menuOne" @click='goUrl("/my/siteManage")'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/fuwudian.png" mode="widthFix"></image>
					</view>
					<text>服务点管理</text>
				</view>
				<view class="menuOne" @click='goUrl("/my/leaderOrder")'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/dingdan.png" mode="widthFix"></image>
					</view>
					<text>订单管理</text>
				</view>
				<view class="menuOne" @click='goUrl("/my/members")'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/tuanyuan.png" mode="widthFix"></image>
					</view>
					<text>团员管理</text>
				</view>
				<view class="menuOne" @click='goUrl("/my/PTList")'>
					<view class="menuIcon imgPublic">
						<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/tuangou.png" mode="widthFix"></image>
					</view>
					<text>团购管理</text>
				</view>
			</view>
		</view>	
		<view class="seat"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageHeader:getApp().globalData.pageHeader,
				pageStatus:getApp().globalData.pageStatus,
				
				invite:'',
				isLogin:false,
				datas:{},
				orderCount:{},
				code:'',
				adv:'',
				device:getApp().globalData.platform,
			};
		},
		onLoad() {
			
		},
		onShow(){
			this.isLogin = getApp().globalData.isLogin;
			if(!this.login){
				wx.login({
					success:(login)=>{
						console.log(login);
						this.code = login.code;
					},
					fail:(err)=>{
						console.log(err);
					}
				})
			}
			if(this.isLogin){
				this.initUser();
				// 订单列表数
				this.initOrderCount();
			}
			this.initAdv();
		},
		methods:{
			initAdv(){
				this.tool.getData('/api/user/getUserImg',{},'',this).then(res=>{
					if(res){
						this.adv = res.img;
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			// 查看团购
			lookTG(){
				uni.navigateTo({
					url:'../my/activeDetail'
				})
			},
			// 获取用户信息
			initUser(){
				this.tool.getData('/api/user/info',{},'',this).then(res=>{
					if(res){
						this.datas = res;
						this.isLogin = true;
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			// 订单列表数
			initOrderCount(){
				this.tool.getData('/api/user/getOrderNum',{},"",this).then(res=>{
					if(res){
						this.orderCount = res;
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			// 一键登录
			getPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权  
					uni.showToast({
						title:'用户拒绝授权',
						icon:"none"
					})
				} else {
					this.tool.getData('/api/weixin/jscode2session',{
						code:this.code
					}).then(res=>{
						if(res){
							this.tool.getData('/api/weixin/programLogin',{
								encrypt: e.detail.encryptedData,
								iv: e.detail.iv,
								session_key: JSON.parse(res).session_key,
								openid: JSON.parse(res).openid,
								// param: this.invite,
								tjr_mobile:uni.getStorageSync('tjr_mobile')?uni.getStorageSync('tjr_mobile'):'',
								unionid:JSON.parse(res).unionid,
								source:'0',
							}).then(res=>{
								if(res){
									uni.setStorageSync('uToken',res.userinfo.token);
									uni.setStorageSync('uId',res.userinfo.id);
									getApp().globalData.isLogin = true;
									uni.setStorageSync('user_level',res.userinfo.user_level);
									this.isLogin = true;
									this.initUser();
									this.initOrderCount();
								}else{
									wx.login({
										success:(info)=>{
											if (info.code) {
												this.tool.getData('/api/weixin/jscode2session',{
													code:info.code
												}).then(res=>{
													if(res){
														this.tool.getData('/api/weixin/programLogin',{
															encrypt: e.detail.encryptedData,
															iv: e.detail.iv,
															session_key: JSON.parse(res).session_key,
															openid: JSON.parse(res).openid,
															param: this.invite,
															unionid:JSON.parse(res).unionid,
															source:'0',
														}).then(res=>{
															if(res){
																uni.setStorageSync('uToken',res.userinfo.token);
																uni.setStorageSync('uId',res.userinfo.id);
																getApp().globalData.isLogin = true;
																uni.setStorageSync('user_level',res.userinfo.user_level);
																this.isLogin = true;
																this.initUser();
																this.initOrderCount();
															}else{
																
																
															}
														}).catch(err=>{
															console.log(err);
														})
													}
												}).catch(err=>{
													console.log(err);
												})
											}
										}
									})
								}
							}).catch(err=>{
								console.log(err);
							})
						}
					}).catch(err=>{
						console.log(err);
					})
				}
			},
			// 动态菜单跳转
			actMenuJump(line){
				console.log(line);
				if(this.isLogin){
					uni.navigateTo({
						url:line.path,
					})
				}else{
					uni.showToast({
						title:'请先登录',
						icon:'none'
					})
				}
			},
			// 收货地址
			goPage(idx,status){
				if(this.isLogin){
					if(idx == 1){//收货地址
						uni.navigateTo({
							url:'../my/address'
						})
					}else if(idx == 2){
						uni.navigateTo({
							url:'../my/cpList',
						})
					}else if(idx == 4){//订单列表
						uni.navigateTo({
							url:'../my/orders?status='+status,
						})
					}else if(idx == 5){//查看个人信息
						uni.navigateTo({
							url:'../my/systemSet'
						})
					}else if(idx == 6){// 申请成为团长 auth_status
						if(this.datas.auth_status == '1'){
							uni.showToast({
								title:'您的申请正在审核中',
								icon:'none'
							})
							return
						}
						if(this.datas.auth_status == '2'){
							uni.showToast({
								title:'您已开通团长身份',
								icon:'none'
							})
							return
						}
						uni.navigateTo({
							url:'../my/settle',
						})
					}
				}else{
					uni.showToast({
						title:'请先登录',
						icon:'none'
					})
				}
			},
			goUrl(url){
				uni.navigateTo({
					url:url,
					fail:(err)=>{
						console.log(err);
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #F6F6F6;
	}
	.my{
		width: 100%;
		box-sizing: border-box;
		.part{
			width: 94%;
			background: white;
			border-radius: 8px;
			margin:0 auto 12px;
			.partTit{
				padding: 12px;
				box-sizing: border-box;
				width: 100%;
				border-bottom: 1px solid #F6F6F6;
				font-weight: bold;
				.teamBtns{
					.team_btn{
						width:max-content;
						font-size:10px;
						color:#808080;
						border-radius: 50px;
						padding:2px 4px;
						border: 1px solid #808080;
						margin-left:6px;
					}
					width:max-content;
				}
				.order{
					font-size: 12px;
					color: #999999;
					.rightIcon{
						width: 16px;
						height: 16px;
						margin-left: 2px;
					}
				}
			}
			.orders{
				padding: 12px 0 0;
				.menuOne{
					width: 25%;
					font-size: 12px;
					text-align: center;
					margin-bottom: 10px;
					position: relative;
					.showCount{
						width: 16px;
						height:16px;
						background:#FD6524;
						color: white;
						text-align: center;
						line-height:16px;
						border-radius: 50%;
						position: absolute;
						right:16px;
						top: 0;
					}
					.menuIcon{
						width: 32px;
						height: 32px;
						margin: 0 auto 2px;
					}
				}
			}
			.content{
				.menuOne{
					display: inline-block;
					width:25%;
					text-align: center;
					.menuIcon{
						width: 36px;
						height: 36px;
						margin: 0 auto 2px;
					}
					button{
						display: inline-block;
						vertical-align: bottom;
						font-size: 12px;
						color: #333333;
					}
				}
			}
		}
		.userCenter{
			width: 100%;
			// background: linear-gradient(180deg, #FFF9E4 0%, white 100%);
			background: white;
			position: relative;
			margin-bottom:32px;
			.myBg{
				width: 100%;
			}
			.advImg{
				width:85%;
				position:absolute;
				top: 65%;
				left: 7.5%;
			}
			.userInfoCenter{
				width: 100%;
				height: 100%;
				position:absolute;
				top: 0;
				left: 0;
				padding: 20px 20px;
				box-sizing: border-box;
				.userInfo{
					flex: 1;
					font-size: 16px;
					color:white;
					.info{
						flex:1;
						.mobile{
							font-size:12px;
							margin-top:5px;
						}
					}
					.userTx{
						width: 52px;
						height: 52px;
						border-radius: 50%;
						overflow: hidden;
						margin-right: 16px;
						image{
							width: 100%;
							height:100%;
						}
					}
				}
				.changeInfo_btn{
					width: max-content;
					font-size: 12px;
					color:white;
					border-radius: 50px;
					padding:4px 8px;
					background: rgba(255, 255, 255, 0.2);
				}
				.loginBtn{
					width: max-content;
					font-size: 12px;
					background: white;
					border-radius: 40px;
					padding: 4px 12px;
					font-size: 12px;
					color:white;
					background: $col_active;
				}
			}
		}
	}
	.header{
		width:100%;
		color:white;
		border-bottom: 1px solid white;
		background:linear-gradient(to right,#fc9700,#ff5000);
	}
</style>
